package com.example.uniappbackend.controller;

import com.example.uniappbackend.entity.Student;
import com.example.uniappbackend.service.StudentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Student)表控制层
 *
 * @author makejava
 * @since 2021-04-07 15:40:09
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    /**
     * 服务对象
     */
    @Resource
    private StudentService studentService;


    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public List<Student> list(@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                              @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {
        if (pageNum < 1) {
            pageNum = 1;
        }
        int offset = (pageNum - 1) * pageSize;
        return this.studentService.queryAllByLimit(offset, pageSize);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public Student selectStudent(@PathVariable Integer id) {
        return this.studentService.queryById(id);
    }

    /**
     * 修改数据
     *
     * @param student
     * @return 单条数据
     */
    @PutMapping
    public Student updateStudent(@RequestBody Student student) {
        return this.studentService.update(student);
    }

    /**
     * 通过主键删除单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @DeleteMapping("/{id}")
    public Object deleteStudent(@PathVariable Integer id) {
        return this.studentService.deleteById(id);
    }

    /**
     * 新增
     *
     * @param student
     * @return 单条数据
     */
    @PostMapping
    public Student insertStudent(@RequestBody Student student) {
        return this.studentService.insert(student);
    }


}
